package com.everydayflash.cart.core {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;	

	/**
	 * @author bartekd
	 */
	public class AbstractSketch extends Sprite {

		protected var canvas:Sprite;
		protected var bcanvas:BitmapData;

		public function AbstractSketch() {
			stage.quality = StageQuality.LOW;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			addEventListener(Event.ADDED_TO_STAGE, onAdded);
		}
		
		protected function onAdded(event:Event):void {

			canvas = new Sprite();
			addChild(canvas);

			bcanvas = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00000000);
			addChild(new Bitmap(bcanvas));
			
			init();
			
			addEventListener(Event.ENTER_FRAME, render);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
		}
		
		protected function clearBCanvas():void {
			bcanvas.fillRect(bcanvas.rect, 0x00000000);
		}

		protected function init():void {
			
		}
		
		protected function mouseDown(e:MouseEvent):void {
		}
		
		private function render(e:Event):void {
			draw();
		}
		
		protected function draw():void {
		}
		
		public function get sw():Number {
			return stage.stageWidth;
		}
		
		public function get sh():Number {
			return stage.stageHeight;
		}
	}
}
